/*
 * Copyright (c) 2013 ICM Uniwersytet Warszawski All rights reserved.
 * See LICENCE.txt file for licensing information.
 */
package pl.edu.icm.unity.webadmin.tprofile.wizard;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import pl.edu.icm.unity.server.authn.remote.RemotelyAuthenticatedInput;
import pl.edu.icm.unity.server.translation.in.InputTranslationProfile;
import pl.edu.icm.unity.server.utils.UnityMessageSource;
import pl.edu.icm.unity.webadmin.tprofile.TranslationProfileEditor;
import pl.edu.icm.unity.webui.common.safehtml.HtmlLabel;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.TableDragMode;
import com.vaadin.ui.VerticalLayout;

/**
 * UI Component used by {@link ProfileStep}.
 * 
 * @author Roman Krysinski
 */
public class ProfileStepComponent extends CustomComponent 
{

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private HorizontalLayout mainLayout;
	@AutoGenerated
	private HorizontalSplitPanel splitPanelLayout;
	@AutoGenerated
	private VerticalLayout leftPanel;
	@AutoGenerated
	private Table attributesTable;
	@AutoGenerated
	private HtmlLabel dragdropHint;
	@AutoGenerated
	private VerticalLayout rightPanel;
	private static final float DEFAULT_SIZE_OF_SPLIT_POS = 50;
	private UnityMessageSource msg;
	private TranslationProfileEditor editor;
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 * @param editor 
	 * @param msg 
	 */
	public ProfileStepComponent(UnityMessageSource msg, TranslationProfileEditor editor) 
	{
		this.msg = msg;
		this.editor = editor;
		
		buildMainLayout();
		setCompositionRoot(mainLayout);
		
		rightPanel.removeAllComponents();
		rightPanel.addComponent(editor);
		
		dragdropHint.setHtmlValue("Wizard.ProfileStepComponent.dragdropHint");
		
		splitPanelLayout.setSplitPosition(DEFAULT_SIZE_OF_SPLIT_POS, Unit.PERCENTAGE, false);
	}
	
	public void handle(RemotelyAuthenticatedInput authnInput) 
	{
		initializeAttributesTable(authnInput);
		editor.setRemoteAuthnInput(authnInput);
	}

	private void initializeAttributesTable(RemotelyAuthenticatedInput input) 
	{
		attributesTable.setDragMode(TableDragMode.ROW);
		attributesTable.setSelectable(true);
		attributesTable.setItemDescriptionGenerator(new Table.ItemDescriptionGenerator() 
		{
			@Override
			public String generateDescription(Component source, Object itemId, Object propertyId) 
			{
	            return itemId.toString();
			}
		});
		BeanItemContainer<DragDropBean> idBeans = new BeanItemContainer<DragDropBean>(DragDropBean.class);
		idBeans.addAll(getTableContent(input));
		attributesTable.setContainerDataSource(idBeans);
		attributesTable.setVisibleColumns(new Object[]{"expression", "value"});
		attributesTable.setColumnHeader("expression", msg.getMessage("Wizard.ProfileStepComponent.expression"));
		attributesTable.setColumnHeader("value", msg.getMessage("Wizard.ProfileStepComponent.value"));
		attributesTable.setPageLength(attributesTable.size());
		attributesTable.refreshRowCache();
	}
	
	private Collection<DragDropBean> getTableContent(RemotelyAuthenticatedInput input)
	{
		Collection<DragDropBean> items = new ArrayList<DragDropBean>();
		Map<String, String> exprValMap = InputTranslationProfile.createExpresionValueMap(input);
		
		for (Map.Entry<String, String> exprE : exprValMap.entrySet())
		{
			items.add(new DragDropBean(exprE.getKey(), exprE.getValue()));
		}
		
		return items;
	}

	@AutoGenerated
	private HorizontalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new HorizontalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setMargin(true);
		
		// top-level component properties
		setWidth("100.0%");
		
		// splitPanelLayout
		splitPanelLayout = buildSplitPanelLayout();
		mainLayout.addComponent(splitPanelLayout);
		mainLayout.setExpandRatio(splitPanelLayout, 1.0f);
		
		return mainLayout;
	}

	@AutoGenerated
	private HorizontalSplitPanel buildSplitPanelLayout() {
		// common part: create layout
		splitPanelLayout = new HorizontalSplitPanel();
		splitPanelLayout.setImmediate(false);
		splitPanelLayout.setWidth("100.0%");
		splitPanelLayout.setHeight("100.0%");
		
		// rightPanel
		rightPanel = new VerticalLayout();
		rightPanel.setImmediate(false);
		rightPanel.setWidth("100.0%");
		rightPanel.setMargin(false);
		splitPanelLayout.addComponent(rightPanel);
		
		// leftPanel
		leftPanel = buildLeftPanel();
		splitPanelLayout.addComponent(leftPanel);
		
		return splitPanelLayout;
	}

	@AutoGenerated
	private VerticalLayout buildLeftPanel() {
		// common part: create layout
		leftPanel = new VerticalLayout();
		leftPanel.setImmediate(false);
		leftPanel.setWidth("100.0%");
		leftPanel.setMargin(true);
		leftPanel.setSpacing(true);
		
		// dragdropHint
		dragdropHint = new HtmlLabel(msg);
		dragdropHint.setImmediate(false);
		dragdropHint.setWidth("-1px");
		dragdropHint.setHeight("-1px");
		leftPanel.addComponent(dragdropHint);
		
		// attributesTable
		attributesTable = new Table();
		attributesTable.setImmediate(false);
		attributesTable.setWidth("100.0%");
		attributesTable.setHeight("100.0%");
		leftPanel.addComponent(attributesTable);
		leftPanel.setExpandRatio(attributesTable, 1.0f);
		
		return leftPanel;
	}
}
